﻿using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using Libraries.Common;
using Libraries.Entities;

namespace Libraries.Repositories
{
    public class CourseFeedBackRepository
    {
        #region Feedback
        private CourseFeedBack ParseToEntityCourseFeedBack(IDataRecord dataReader)
        {
            if (dataReader != null)
            {
                return new CourseFeedBack()
                {
                    CourseId = dataReader["CourseId"].ToInt32OrDefault(),
                    Date = dataReader["Date"].ToDateTimeOrDefault(),
                    AppropriateCourseLevel = dataReader["3"].ToStringOrDefault(),
                    CourseObjectIsMet = dataReader["2"].ToStringOrDefault(),
                    Instructor = dataReader["Instructor"].ToStringOrDefault(),
                    CourseUsefullness = dataReader["4"].ToStringOrDefault(),
                    GoodInstructionAndCommunication = dataReader["9"].ToStringOrDefault(),
                    OverAllScore = dataReader["Average"].ToStringOrDefault(),
                    SubjectCoverage = dataReader["7"].ToStringOrDefault(),
                    Topic = dataReader["TopicName"].ToStringOrDefault(),
                    TrainerSupporting = dataReader["8"].ToStringOrDefault(),
                    TrainingMaterialQuality = dataReader["5"].ToStringOrDefault()

                };
            }
            return null;
        }
        public Collection<CourseFeedBack> CourseFeedBacks(int courseId)
        {
            var parameters = new Collection<SqlParameter>
                                 {
                                     new SqlParameter("@CourseId",courseId)
                                 };

            Collection<CourseFeedBack> list = null;
            if (!string.IsNullOrEmpty(ProcedureConfiguration.FeedbackCourseFetchByCourseId))
            {
                using (var result = RepositorySql.SqlHelper.ExecuteReader(ProcedureConfiguration.FeedbackCourseFetchByCourseId, parameters, ConnectionConfiguration.ConnectionString))
                {
                    if (result != null)
                    {
                        list = new Collection<CourseFeedBack>();
                        while (result.Read())
                        {
                            list.Add(ParseToEntityCourseFeedBack(result));
                        }
                    }
                }
            }
            return list;
        }
        #endregion 
    }
}